/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tr.com.argela.tap.mot.dao;

import java.sql.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author arge
 */
public class DbManager {

    DataSource dataSource;
    Connection conn;

    //public static Logger logger = Logger.getLogger("motLogger");

    public DbManager() {

         try {

            dataSource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/mot");

            conn = dataSource.getConnection();

            conn.setAutoCommit(false);

            System.out.print("[DB] - Connection created, " + conn.toString());

        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
   
    }

      public void commit() {

        try {

            System.out.print("[DB] - Connection commit, " + conn.toString());

            conn.commit();

        } catch (SQLException ex) {

            System.out.print("[DB] - FATAL ERROR Could not commit connection " + ex.getMessage());

        }

    }

 

    public Connection getConn() {

        return conn;

    }



    public void close() {

        try {

            conn.close();

            System.out.print("[DB] - Connection closed " + new java.util.Date().toString());

        } catch (SQLException e) {

            System.out.print("[DB] - FATAL ERROR Could not close connection " + e.getMessage());

        }

    }



    public void rollback() {

        try {

            conn.rollback();

        } catch (SQLException e) {

            System.out.print("[DB] - FATAL ERROR Could not rollback connection " + e.getMessage());

        }

    }

}
